package com.smallsoft.phongtucvietnam;

import com.smallsoft14.phongtucvietnam.R;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ProgressBar;

public class DetailFragment extends Fragment{

	ViewGroup viewGroup;
	WebView webView;
	String html;
	Handler mHandler = new Handler();
	ProgressBar progressBar;
	ProgressDialog progressDialog;
	String content;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		content = getArguments().getString("content");
		html = getHTML(content);
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
	if(viewGroup == null){
		viewGroup = (ViewGroup)inflater.inflate(R.layout.detail, container, false);
		webView = (WebView)viewGroup.findViewById(R.id.web_view);
		progressBar = (ProgressBar)viewGroup.findViewById(R.id.progress_bar);
		mHandler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				// ---0 - VISIBLE; 4 - INVISIBLE; 8 - GONE---
				progressBar.setVisibility(8);
			}
		}, 200);
		webView.loadDataWithBaseURL("", html, "text/html", "utf-8", null);
		
	}else{
		((ViewGroup)this.viewGroup.getParent()).removeView(this.viewGroup);
	}
	return viewGroup;
	}
	
	private String getHTML(String content){
		String html = "";
		html = "<html><head>"
				+
				"<meta name=\"viewport\" content=\"width=device-width,initial-scale=1,user-scalable=no\" />"
				+
				"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />"
				+
				"<style type=\"text/css\" rel=\"stylesheet\">"
				+
				"body {font-family: arial, helvetica, sans-serif;}"
				+
				".content{text-align: justify; margin: 0; color: #555; line-height: 1.5; font-size: medium; background: #fff;}"
				+
				"</style>"
				+
				"<head>"
				+
				"<body>"
				+
				"<div class=\"content\">"
				+
				 content
				+
				"</div>"
				+
				"</body></html>";
		return html;
	}
}
